#include "chatmessageitem.h"

#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFrame>

static inline QString kGreen() { return QStringLiteral("#66cc66"); }
static inline QString kIvory() { return QStringLiteral("#fffff0"); }
static inline QString kBlack() { return QStringLiteral("#000000"); }

ChatMessageItem::ChatMessageItem(SenderKind sender,
                                 const QString &senderName,
                                 const QString &messageText,
                                 QWidget *parent)
    : QWidget(parent)
{
    buildUi(sender, senderName, messageText);
}

QString ChatMessageItem::selfBackgroundColor() { return kGreen(); }
QString ChatMessageItem::selfBorderColor() { return kGreen(); }
QString ChatMessageItem::peerBackgroundColor() { return kIvory(); }
QString ChatMessageItem::peerBorderColor() { return kIvory(); }

void ChatMessageItem::buildUi(SenderKind sender,
                              const QString &senderName,
                              const QString &messageText)
{
    auto *root = new QHBoxLayout(this);
    root->setContentsMargins(8, 8, 8, 8);
    root->setSpacing(8);

    const bool isSelf = (sender == SelfSide);
    const QString bg = isSelf ? selfBackgroundColor() : QStringLiteral("transparent");
    const QString border = isSelf ? selfBorderColor() : QStringLiteral("transparent");

    QWidget *nameBadge = createNameBadgePlain(senderName);
    QWidget *bubble = createBubble(messageText, bg, border,
                                   isSelf /*drawBorder*/,
                                   isSelf ? Qt::AlignRight : Qt::AlignLeft);

    if (isSelf) {
        root->addStretch();
        root->addWidget(bubble, 0, Qt::AlignRight | Qt::AlignVCenter);
        root->addWidget(nameBadge, 0, Qt::AlignRight | Qt::AlignVCenter);
    } else {
        root->addWidget(nameBadge, 0, Qt::AlignLeft | Qt::AlignVCenter);
        root->addWidget(bubble, 0, Qt::AlignLeft | Qt::AlignVCenter);
        root->addStretch();
    }
}

QWidget *ChatMessageItem::createNameBadgePlain(const QString &name)
{
    auto *badgeFrame = new QWidget(this);
    auto *layout = new QHBoxLayout(badgeFrame);
    layout->setContentsMargins(0, 0, 0, 0);
    auto *label = new QLabel(name, badgeFrame);
    QFont f = label->font();
    f.setPointSize(std::max(9, f.pointSize()-1));
    label->setFont(f);
    label->setStyleSheet(QString("QLabel{ color:%1; background:transparent; }").arg(kBlack()));
    layout->addWidget(label);
    return badgeFrame;
}

QWidget *ChatMessageItem::createBubble(const QString &text,
                                       const QString &background,
                                       const QString &borderColor,
                                       bool drawBorder,
                                       Qt::Alignment alignment)
{
    auto *frame = new QFrame(this);
    frame->setFrameShape(QFrame::NoFrame);
    if (drawBorder) {
        frame->setStyleSheet(QString("QFrame{ border:1px solid %1; background:%2; } QLabel{ color:%3; }")
                             .arg(borderColor, background, kBlack()));
    } else {
        frame->setStyleSheet(QString("QFrame{ border:none; background:%1; } QLabel{ color:%2; }")
                             .arg(background, kBlack()));
    }

    auto *layout = new QVBoxLayout(frame);
    layout->setContentsMargins(8, 8, 8, 8);
    auto *label = new QLabel(text, frame);
    label->setWordWrap(true);
    layout->addWidget(label, 0, alignment);
    return frame;
}


